import { create } from 'zustand';

export interface Document {
  id: string;
  name: string;
  type: string;
  size: string;
  creator: string;
  createTime: string;
  updateTime: string;
  status: string;
  tags: string[];
  description: string;
  category: string;
  path: string;
}

interface Folder {
  id: string;
  name: string;
  parentId: string | null;
  createTime: string;
  updateTime: string;
  creator: string;
}

interface DocumentState {
  documents: Document[];
  folders: Folder[];
  loading: boolean;
  selectedDocument: Document | null;
  currentFolder: string | null;
  fetchDocuments: () => Promise<void>;
  selectDocument: (document: Document | null) => void;
  setCurrentFolder: (folderId: string | null) => void;
}

export const useDocumentStore = create<DocumentState>((set) => ({
  documents: [],
  folders: [],
  loading: false,
  selectedDocument: null,
  currentFolder: null,
  
  fetchDocuments: async () => {
    set({ loading: true });
    
    try {
      // 模拟API请求
      await new Promise(resolve => setTimeout(resolve, 500));
      
      // 模拟文档数据
      const mockDocuments: Document[] = Array.from({ length: 20 }).map((_, index) => ({
        id: `doc-${index}`,
        name: `测试文档 ${index}`,
        type: ['PDF', 'Word', 'Excel', 'PPT', 'Text'][Math.floor(Math.random() * 5)],
        size: `${Math.floor(Math.random() * 1000) + 100}KB`,
        creator: `用户${Math.floor(Math.random() * 5)}`,
        createTime: new Date(Date.now() - Math.random() * 90 * 24 * 60 * 60 * 1000).toISOString(),
        updateTime: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(),
        status: ['草稿', '已发布', '已归档'][Math.floor(Math.random() * 3)],
        tags: Array.from({ length: Math.floor(Math.random() * 4) + 1 }).map(() => 
          ['重要', '财务', '合同', '营销', '技术', '人力资源', '法律'][Math.floor(Math.random() * 7)]
        ),
        description: `这是一份测试文档，包含了各种信息和数据。文档编号 ${index}，用于测试系统功能。`,
        category: ['财务', '人力资源', '市场营销', '技术文档', '合同'][Math.floor(Math.random() * 5)],
        path: `/文档/${['财务', '人力资源', '市场营销', '技术文档', '合同'][Math.floor(Math.random() * 5)]}`,
      }));
      
      // 模拟文件夹数据
      const mockFolders: Folder[] = [
        { id: 'folder-1', name: '财务文档', parentId: null, createTime: '2023-06-01', updateTime: '2023-06-10', creator: '用户1' },
        { id: 'folder-2', name: '人力资源', parentId: null, createTime: '2023-06-02', updateTime: '2023-06-11', creator: '用户2' },
        { id: 'folder-3', name: '市场营销', parentId: null, createTime: '2023-06-03', updateTime: '2023-06-12', creator: '用户3' },
        { id: 'folder-4', name: '技术文档', parentId: null, createTime: '2023-06-04', updateTime: '2023-06-13', creator: '用户4' },
        { id: 'folder-5', name: '合同', parentId: null, createTime: '2023-06-05', updateTime: '2023-06-14', creator: '用户5' },
      ];
      
      set({ 
        documents: mockDocuments, 
        folders: mockFolders,
        loading: false 
      });
    } catch (error) {
      console.error('获取文档失败:', error);
      set({ loading: false });
    }
  },
  
  selectDocument: (document) => {
    set({ selectedDocument: document });
  },
  
  setCurrentFolder: (folderId) => {
    set({ currentFolder: folderId });
  },
})); 